package be.khleuven.screencasting.Facades;

import be.khleuven.screencasting.entiteiten.*;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author esp
 */

@Stateless
public class QueryFacadeRemoteImpl implements QueryFacadeRemote {

    @PersistenceContext
    private EntityManager eManager;
    

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Object create(Object obj) {
        eManager.persist(obj);
        eManager.flush();
        return obj;
    }

    public Persoon getPersoon(String login,String passwd) {
        String query = "select pers from Persoon pers where pers.login=:login_waarde and pers.password=:passwd";
        Query q = eManager.createQuery(query).setParameter("login_waarde",login);
        q.setParameter("passwd", passwd);

        Persoon pers = null;
        try {
            pers = (Persoon) q.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        return pers;
    }

    public Screencast getScreencast(Long id) {
        String query = "select s from Screencast s where s.id=:id_waarde" ;
        Query q = eManager.createQuery(query).setParameter("id_waarde", id) ;

        Screencast scr = null ;

        try {
            scr = (Screencast)  q.getSingleResult();
        }
        catch (NoResultException e) {
            return null ;
        }
        
        return scr ;
    }
}